module Piston
  module Core
    class XamlBuilder < System::Xml::XmlDocument
      attr_accessor :namespace_manager

      def initialize(xaml_file)
        super()
        load xaml_file

        @namespace_manager = System::Xml::XmlNamespaceManager.new(name_table)

        { "xaml" => "http://schemas.microsoft.com/winfx/2006/xaml/presentation",
          "x" => "http://schemas.microsoft.com/winfx/2006/xaml",
          "avalonDock" => "http://avalondock.codeplex.com"
        }.each_pair do |ns, url|
          @namespace_manager.add_namespace(ns, url)
        end
      end

      def append(xpath, file)
        reader = System::IO::StreamReader.new(file)
        nodes = select_nodes(xpath, @namespace_manager)
        fragment = create_document_fragment()
        fragment.inner_xml = reader.read_to_end()
        puts "nodes=#{nodes.count}"
        nodes.each do |node|
          node.append_child fragment
        end
      end
    end
  end
end